import axios from 'axios'
import { message } from 'antd'
import Cookie from 'js-cookie'
import config from 'config/config.js'
// 请求拦截统一增加参数
axios.interceptors.request.use(config => {
	config.timeout = 10 * 1000
	return config
}, error => {
	return Promise.reject(error)
})

axios.interceptors.response.use(response => {
	if('' + response.data.resultCode === '0') {
		return Promise.resolve(response.data)
	}
	message.error(response.data.resultMsg)
	return Promise.reject(response.data)
})



// 请求方法
export default function request(params) {
	const defautParam = {
    url: "",
    method: 'GET',
    headers: {
      "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
      // 每一个请求头都要加上token值
      "token": Cookie.get(config.cookie.token) || ''
    },
    transformRequest: [function (data) {
	    // 将数据转换为表单数据
	    let ret = ''
	    for (let it in data) {
	      ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
	    }
	    return ret
	  }],
    data: {}
  }
	params.method = params.method.toLocaleLowerCase()

	params.headers = Object.assign({}, defautParam.headers, params.headers)

	const param = Object.assign({}, defautParam, params)

	if(param.url === '') {
		console.error('请求路径不能为空')
		return
	}

	return axios(param)
}
